package com.androidx.p013lv.base.utils;

import com.androidx.p013lv.base.BaseApp;
import com.androidx.p013lv.base.bean.AdBaseBean;
import com.androidx.p013lv.base.bean.AdInfoBean;
import com.androidx.p013lv.base.bean.AdListBean;
import com.androidx.p013lv.base.p014db.AdBaseBeanDao;
import com.androidx.p013lv.base.p014db.AdInfoBeanDao;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import org.greenrobot.greendao.query.WhereCondition;

/* renamed from: com.androidx.lv.base.utils.AdUtils */
public class AdUtils {
    static AdUtils adUtils;

    public static AdUtils getInstance() {
        if (adUtils == null) {
            adUtils = new AdUtils();
        }
        return adUtils;
    }

    public AdInfoBeanDao getAdInfoBeanDao() {
        return BaseApp.getDaoSession().getAdInfoBeanDao();
    }

    public AdBaseBeanDao getAdBaseListBeanDao() {
        return BaseApp.getDaoSession().getAdBaseBeanDao();
    }

    public void deleteALL() {
        getAdInfoBeanDao().deleteAll();
        getAdBaseListBeanDao().deleteAll();
    }

    public void putAdBean(AdListBean adListBean) {
        AdInfoBeanDao adInfoBeanDao = getAdInfoBeanDao();
        adInfoBeanDao.deleteAll();
        AdBaseBeanDao adBaseListBeanDao = getAdBaseListBeanDao();
        adBaseListBeanDao.deleteAll();
        if (adListBean != null) {
            if (adListBean.getAdvertisementInfos() != null && adListBean.getAdvertisementInfos().size() > 0) {
                adInfoBeanDao.insertInTx(adListBean.getAdvertisementInfos());
            }
            if (adListBean.getAdvertisementPlaces() != null && adListBean.getAdvertisementPlaces().size() > 0) {
                adBaseListBeanDao.insertInTx(adListBean.getAdvertisementPlaces());
            }
        }
    }

    public AdInfoBean getNovelAd() {
        return getAdInfoBeanWeight("APPOINTMENT", 0, 1);
    }

    public AdInfoBean getVoiceNovelAd() {
        return getAdInfoBeanWeight("APPOINTMENT", 0, 2);
    }

    public AdInfoBean getSplash() {
        return getAdInfoBeanWeight("START", 0, 0);
    }

    public AdInfoBean getHomeWeight(int i) {
        return getAdInfoBeanWeight("HOME", 0, i);
    }

    public List<AdInfoBean> getHomeBanner(int i) {
        return getAdInfoBeanSort("HOME", 0, i);
    }

    public AdInfoBean getVideoAd() {
        return getAdInfoBeanWeight("SMALL_VIDEO", 2, 0);
    }

    public int getRuleIntervalNum() {
        AdBaseBean adBaseBean = getAdBaseBean("SMALL_VIDEO", "WEIGHT", 0);
        if (adBaseBean == null || adBaseBean.getRuleIntervalNum() <= 0) {
            return 6;
        }
        return adBaseBean.getRuleIntervalNum();
    }

    public AdInfoBean getAd(String str) {
        return getAdInfoBeanWeight(str, 2, 0);
    }

    public int getAdIntervalNum(String str) {
        AdBaseBean adBaseBean = getAdBaseBean(str, "WEIGHT", 0);
        if (adBaseBean == null || adBaseBean.getRuleIntervalNum() <= 0) {
            return 6;
        }
        return adBaseBean.getRuleIntervalNum();
    }

    public int getRuleHomeNum(int i) {
        AdBaseBean adBaseBean = getAdBaseBean("HOME", "WEIGHT", i);
        if (adBaseBean == null || adBaseBean.getRuleIntervalNum() <= 0) {
            return 6;
        }
        return adBaseBean.getRuleIntervalNum();
    }

    public AdInfoBean getPlay() {
        return getAdInfoBeanWeight("PLAY_PAGE", 0, 0);
    }

    public List<AdInfoBean> getAdSort(String str) {
        return getAdInfoBeanSort(str, 0, 0);
    }

    public List<AdInfoBean> getAppointment() {
        return getAdInfoBeanSort("APPOINTMENT", 0, 0);
    }

    private List<AdInfoBean> getAdInfoBeanSort(String str, int i, int i2) {
        AdBaseBean adBaseBean = getAdBaseBean(str, "SEQUENCE", i2);
        if (adBaseBean == null) {
            return null;
        }
        return getAdInfoBeansDesc(adBaseBean.getId(), i);
    }

    private AdInfoBean getAdInfoBeanWeight(String str, int i, int i2) {
        List<AdInfoBean> adInfoBeans;
        AdBaseBean adBaseBean = getAdBaseBean(str, "WEIGHT", i2);
        if (adBaseBean == null || (adInfoBeans = getAdInfoBeans(adBaseBean.getId(), i)) == null || adInfoBeans.size() <= 0) {
            return null;
        }
        return weightRandom(adInfoBeans);
    }

    private AdBaseBean getAdBaseBean(String str, String str2, int i) {
        List list;
        if (i > 0) {
            list = getAdBaseListBeanDao().queryBuilder().where(AdBaseBeanDao.Properties.AdPlaceEnum.mo36808eq(str), AdBaseBeanDao.Properties.LoadTypeEnum.mo36808eq(str2), AdBaseBeanDao.Properties.ClassifyId.mo36808eq(Integer.valueOf(i))).build().list();
        } else {
            list = getAdBaseListBeanDao().queryBuilder().where(AdBaseBeanDao.Properties.AdPlaceEnum.mo36808eq(str), AdBaseBeanDao.Properties.LoadTypeEnum.mo36808eq(str2)).build().list();
        }
        if (list == null || list.size() <= 0) {
            return null;
        }
        return (AdBaseBean) list.get(0);
    }

    private List<AdInfoBean> getAdInfoBeans(String str, int i) {
        List<AdInfoBean> list;
        if (i == 0) {
            list = getAdInfoBeanDao().queryBuilder().where(AdInfoBeanDao.Properties.PlaceId.mo36808eq(str), new WhereCondition[0]).where(AdInfoBeanDao.Properties.AdType.mo36808eq("IMG"), new WhereCondition[0]).build().list();
        } else if (i == 2) {
            list = getAdInfoBeanDao().queryBuilder().where(AdInfoBeanDao.Properties.PlaceId.mo36808eq(str), new WhereCondition[0]).whereOr(AdInfoBeanDao.Properties.AdType.mo36808eq("IMG"), AdInfoBeanDao.Properties.AdType.mo36808eq("VIDEO"), new WhereCondition[0]).build().list();
        } else {
            list = getAdInfoBeanDao().queryBuilder().where(AdInfoBeanDao.Properties.PlaceId.mo36808eq(str), new WhereCondition[0]).where(AdInfoBeanDao.Properties.AdType.mo36808eq("VIDEO"), new WhereCondition[0]).build().list();
        }
        Date date = new Date();
        ArrayList arrayList = new ArrayList();
        for (AdInfoBean adInfoBean : list) {
            if (date.getTime() >= adInfoBean.getAdStartTime().getTime() && date.getTime() <= adInfoBean.getAdStopTime().getTime()) {
                arrayList.add(adInfoBean);
            }
        }
        return arrayList;
    }

    private List<AdInfoBean> getAdInfoBeansDesc(String str, int i) {
        List<AdInfoBean> list;
        if (i == 0) {
            list = getAdInfoBeanDao().queryBuilder().where(AdInfoBeanDao.Properties.PlaceId.mo36808eq(str), new WhereCondition[0]).where(AdInfoBeanDao.Properties.AdType.mo36808eq("IMG"), new WhereCondition[0]).build().list();
        } else if (i == 2) {
            list = getAdInfoBeanDao().queryBuilder().where(AdInfoBeanDao.Properties.PlaceId.mo36808eq(str), new WhereCondition[0]).whereOr(AdInfoBeanDao.Properties.AdType.mo36808eq("IMG"), AdInfoBeanDao.Properties.AdType.mo36808eq("VIDEO"), new WhereCondition[0]).build().list();
        } else {
            list = getAdInfoBeanDao().queryBuilder().where(AdInfoBeanDao.Properties.PlaceId.mo36808eq(str), new WhereCondition[0]).where(AdInfoBeanDao.Properties.AdType.mo36808eq("VIDEO"), new WhereCondition[0]).build().list();
        }
        Date date = new Date();
        ArrayList arrayList = new ArrayList();
        for (AdInfoBean adInfoBean : list) {
            if (date.getTime() >= adInfoBean.getAdStartTime().getTime() && date.getTime() <= adInfoBean.getAdStopTime().getTime()) {
                arrayList.add(adInfoBean);
            }
        }
        return arrayList;
    }

    public AdInfoBean weightRandom(List<AdInfoBean> list) {
        if (list != null && list.size() > 0) {
            int i = 0;
            for (AdInfoBean adInfoBean : list) {
                i += adInfoBean.getAdWeight();
            }
            if (i <= 0) {
                return null;
            }
            Integer valueOf = Integer.valueOf(new Random().nextInt(i));
            Integer num = 0;
            for (AdInfoBean adInfoBean2 : list) {
                if (num.intValue() <= valueOf.intValue() && valueOf.intValue() < num.intValue() + adInfoBean2.getAdWeight()) {
                    return adInfoBean2;
                }
                num = Integer.valueOf(num.intValue() + adInfoBean2.getAdWeight());
            }
        }
        return null;
    }
}
